# encoding:utf-8

import sys

map_init = [[2, 2, 1, 1, 1, 2, 2],
            [2, 2, 1, 1, 1, 2, 2],
            [1, 1, 1, 1, 1, 1, 1],
            [1, 1, 1, 0, 1, 1, 1],
            [1, 1, 1, 1, 1, 1, 1],
            [2, 2, 1, 1, 1, 2, 2],
            [2, 2, 1, 1, 1, 2, 2]]


def print_matrix(m):
    for i in m:
        print(i)
    print('')


print_matrix(map_init)


def check_target(m):
    s = 0
    for i in m:
        for j in i:
            if j == 1:
                s += 1
    if s == 1:
        return True
    else:
        return False


# 格式：[[r,c,方向]]
move_list = []


def print_move(ml):
    # todo
    pass


def find_next():
    for r in range(len(map_init)):
        for c in range(len(map_init[r])):
            if map_init[r][c] == 1:
                # 上
                if r > 1 and map_init[r - 1][c] == 1 and map_init[r - 2][c] == 0:
                    # 执行
                    move_list.append([r + 1, c + 1, '上'])
                    map_init[r - 2][c] = 1
                    map_init[r - 1][c] = 0
                    map_init[r][c] = 0
                    # 检查
                    if check_target(map_init):
                        print_matrix(move_list)
                        sys.exit(0)
                    # 递归
                    find_next()
                    # 回溯
                    move_list.pop(-1)
                    map_init[r - 2][c] = 0
                    map_init[r - 1][c] = 1
                    map_init[r][c] = 1
                # 下
                if r < 5 and map_init[r + 1][c] == 1 and map_init[r + 2][c] == 0:
                    # 执行
                    move_list.append([r + 1, c + 1, '下'])
                    map_init[r + 2][c] = 1
                    map_init[r + 1][c] = 0
                    map_init[r][c] = 0
                    # 检查
                    if check_target(map_init):
                        print_matrix(move_list)
                        sys.exit(0)
                    # 递归
                    find_next()
                    # 回溯
                    move_list.pop(-1)
                    map_init[r + 2][c] = 0
                    map_init[r + 1][c] = 1
                    map_init[r][c] = 1
                # 左
                if c > 1 and map_init[r][c - 1] == 1 and map_init[r][c - 2] == 0:
                    # 执行
                    move_list.append([r + 1, c + 1, '左'])
                    map_init[r][c - 2] = 1
                    map_init[r][c - 1] = 0
                    map_init[r][c] = 0
                    # 检查
                    if check_target(map_init):
                        print_matrix(move_list)
                        sys.exit(0)
                    # 递归
                    find_next()
                    # 回溯
                    move_list.pop(-1)
                    map_init[r][c - 2] = 0
                    map_init[r][c - 1] = 1
                    map_init[r][c] = 1
                # 右
                if c < 5 and map_init[r][c + 1] == 1 and map_init[r][c + 2] == 0:
                    # 执行
                    move_list.append([r + 1, c + 1, '右'])
                    map_init[r][c + 2] = 1
                    map_init[r][c + 1] = 0
                    map_init[r][c] = 0
                    # 检查
                    if check_target(map_init):
                        print_matrix(move_list)
                        sys.exit(0)
                    # 递归
                    find_next()
                    # 回溯
                    move_list.pop(-1)
                    map_init[r][c + 2] = 0
                    map_init[r][c + 1] = 1
                    map_init[r][c] = 1
    pass


find_next()
